﻿using System;
using System.Linq;
using BehavioralPatterns.Visitor.DataProcessors;
using RealisticDependencies;

namespace BehavioralPatterns.Visitor.Visitors {
    public class MarketResearchReportVisitor : IVisitor<MarketResearchReport> {
        private readonly IApplicationLogger _logger;

        public MarketResearchReportVisitor(IApplicationLogger logger) {
            _logger = logger;
        }
        
        public MarketResearchReport Visit(FloristDataProcessor processor) {
            var customerProfiles = processor.GetMonthlyCustomerProfiles();
            var customerAges = customerProfiles.Select(customer => customer.Age).ToList();
            var averageAge = (decimal) Math.Round(customerAges.Average(), 2);
            var youngestAge = customerAges.Min();
            var oldestAge = customerAges.Max();

            _logger.LogInfo("Visiting Florist for Generating Market Report", ConsoleColor.Green);

            var report = new MarketResearchReport(_logger);
            
            report.SetData(customerProfiles.Count, averageAge, oldestAge, youngestAge);

            return report;
        }

        public MarketResearchReport Visit(BakeryDataProcessor order) {
            var customerProfiles = order.GetMonthlyCustomerProfiles();
            var customerAges = customerProfiles.Select(customer => customer.Age).ToList();
            var averageAge = (decimal) Math.Round(customerAges.Average(), 2);
            var youngestAge = customerAges.Min();
            var oldestAge = customerAges.Max();

            _logger.LogInfo("Visiting Bakery for Generating Market Report", ConsoleColor.Green);

            var report = new MarketResearchReport(_logger);
            
            report.SetData(customerProfiles.Count, averageAge, oldestAge, youngestAge);
            
            return report;
        }

        public MarketResearchReport Visit(FarmerDataProcessor order) {
            _logger.LogDebug("Market research is not yet available for Farm Data");
            return null;
        }
    }

    public class MarketResearchReport : Report {

        public MarketResearchReport(IApplicationLogger logger) : base(logger) {
            CreatedBy = "MarketResearchRobot";
            CreatedOn = DateTime.UtcNow;
        }
        
        public int NumberOfSamples { get; set; }
        public decimal AverageAge { get; set; }
        public int OldestCustomer { get; set; }
        public int YoungestCustomer { get; set; }

        public void SetData(int sampleCount, decimal averageAge, int oldest, int youngest) {
            NumberOfSamples = sampleCount;
            AverageAge = averageAge;
            OldestCustomer = oldest;
            YoungestCustomer = youngest;
        }
        
        public override void Print() {
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            _logger.LogInfo($"=== 📊 Market Research for {CreatedOn} 📊 ===", ConsoleColor.Cyan);
            _logger.LogInfo($"=== Generated By: {CreatedBy} ===", ConsoleColor.Cyan);
            _logger.LogInfo($"- Number of Samples Today: {NumberOfSamples}", ConsoleColor.Cyan);
            _logger.LogInfo($"- Average Customer Age: {AverageAge}", ConsoleColor.Cyan);
            _logger.LogInfo($"- Oldest Customer: {OldestCustomer}", ConsoleColor.Cyan);
            _logger.LogInfo($"- Youngest Customer: {YoungestCustomer}", ConsoleColor.DarkCyan);
            _logger.LogInfo("\n");
        }
    }
}
